package cn.virens.web.controller.manage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.virens.web.common.BaseUserController;
import cn.virens.web.components.shiro.ShiroRealmInterface;

@RequestMapping("/manage")
@Controller("ManageIndexContorller")
public class IndexContorller extends BaseUserController {

	@Autowired
	@Qualifier("shiro-simple-realm")
	private ShiroRealmInterface mShiroRealmInterface;

	public IndexContorller() {
		super("/manage");
	}

	@RequestMapping("index.jspx")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		return returnUrl("index");
	}

	@RequestMapping(value = "login.jspx")
	public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
		return returnUrl("login");
	}

	@ResponseBody
	@RequestMapping("login/refresh.json")
	public Model refresh(HttpServletRequest request, HttpServletResponse response, Model model) {
		this.setAttribute(request, LOGIN_USER, null);
		this.setAttribute(request, LOGIN_ROLE, null);

		this.mShiroRealmInterface.clearAuthorizationInfo(getSubject(request));

		return renderSuccess(model);
	}
}
